package com.hnkywl.sts.service.base;

import com.hnkywl.sts.dao.base.SjzdsyDao;
import com.hnkywl.sts.dao.base.SjzdzDao;
import com.hnkywl.sts.entity.base.Sjzdsy;
import com.hnkywl.sts.entity.base.Sjzdz;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 数据字典索引service类
 * User: Administrator
 * Date: 13-3-25
 * Time: 下午3:12
 * To change this template use File | Settings | File Templates.
 */
@Service
public class SjzdsyService {
    private SjzdsyDao sjzdsyDao;
    private SjzdzDao sjzdzDao;
    private Long sjzdsyId;

    /**
     * 获取对应索引下的所有数据字典值
     *
     * @param bz
     * @return
     */
    public String findSjzdsyJson(String bz) {
        List<Sjzdz> sjzdzList = findSjzdsy(bz);
        return "[" + BaseUtils.fetchCollectionToJson(sjzdzList, "z", "id","bm") + "]";
    }


    public List<Sjzdz> findSjzdsy(String bz) {
        return sjzdsyDao.findSjzdsy(bz).get(0).getGlz();
    }

    /**
     * 获取所有数据字典索引
     *
     * @param mc
     * @param bm
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllSjzdsy(String mc, String bm, int pageNo, int pageSize) {
        Page<Sjzdsy> sjzdsyPage = this.sjzdsyDao.findSjzdsyList(mc, bm, pageNo, pageSize);
        List<Sjzdsy> sjzdsyList = sjzdsyPage.getResult() == null ? new ArrayList<Sjzdsy>() : sjzdsyPage.getResult();
        String sjzdsyJson = BaseUtils.fetchCollectionToJson(sjzdsyList, "id", "mc", "bm", "bz", "lx");
        return "{\"total\":" + sjzdsyPage.getTotalCount() + ",\"rows\":[" + sjzdsyJson + "]}";
    }

    @Transactional
    public String findSjzdz(Long sjzdsyId) {
        List<Sjzdz> sjzdzList = sjzdzDao.findSjzdzById(sjzdsyId) == null ? new ArrayList<Sjzdz>() : sjzdzDao.findSjzdzById(sjzdsyId);
        String sjzdzJson = BaseUtils.fetchCollectionToJson(sjzdzList, "id", "bz", "bm", "z");
        return "[" + sjzdzJson + "]";
    }

    /**
     * 查询数据字典明细
     *
     * @param id ID
     * @return
     */
    @Transactional
    public Sjzdsy findSjzdsyDetail(Long id) {
        return this.sjzdsyDao.get(id);
    }



    /**
     * 保存数据字典信息
     *
     * @param sjzdsy 实体
     * @return
     */
    @Transactional
    public void saveSjzdsy(Sjzdsy sjzdsy) {
        if (sjzdsyDao.checkUnique(sjzdsy.getId(), sjzdsy.getMc(), "mc")) {
            throw new RuntimeException("数据字典索引名称不能重复！");
        }
        sjzdsy.setDateCreated(new Date());
        sjzdsy.setLastUpdated(sjzdsy.getDateCreated());
        if (sjzdsy.getGlz() != null) {
            for (Sjzdz sjzdz : sjzdsy.getGlz()) {
                sjzdz.setSjzdsy(sjzdsy);
            }
        }
        this.sjzdsyDao.save(sjzdsy);
    }

    /**
     * 更新数据字典 索引
     *
     * @param
     * @return
     */
    @Transactional
    public void updateSjzdsy(Sjzdsy sjzdsy) throws RuntimeException {
        Sjzdsy clone = sjzdsyDao.get(sjzdsy.getId());
        clone = (Sjzdsy) ReflectionUtils.getInstance(clone, sjzdsy);
        clone.setLastUpdated(new Date());
        if (sjzdsy.getGlz() != null) {
            for (Sjzdz sjzdz : sjzdsy.getGlz()) {
                sjzdz.setSjzdsy(sjzdsy);
            }
        }
        this.sjzdsyDao.save(clone);
    }

    /**
     * 删除数据字典索引信息
     *
     * @param
     * @return
     */
    @Transactional
    public void deleteSjzdsy(Long id) {
        Sjzdsy sjzdsy = sjzdsyDao.get(id);
        sjzdsy.setLastUpdated(new Date());
        sjzdsy.setDeleted(1);
        this.sjzdsyDao.save(sjzdsy);
    }

    /**
     * ajax远程验证
     *
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id, String value, String field) {
        return sjzdsyDao.checkUnique(id, value, field);
    }

    public Long getSjzdsyId() {
        return sjzdsyId;
    }

    public void setSjzdsyId(Long sjzdsyId) {
        this.sjzdsyId = sjzdsyId;
    }

    @Autowired
    public void setSjzdsyDao(SjzdsyDao sjzdsyDao) {
        this.sjzdsyDao = sjzdsyDao;
    }

    @Autowired
    public void setSjzdzDao(SjzdzDao sjzdzDao) {
        this.sjzdzDao = sjzdzDao;
    }
}
